package com.sudo.phone;

import com.alibaba.fastjson2.JSON;
import lombok.Data;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author：DayDayUp
 * @Create：2023/2/8 16:43
 */
public class PhoneJsoupUtil {

    /**
     * 公共路径url
     */
    private static String url = "https://www.gouhaowang.com/shoujihaoduan?page=";
    private static Map<String, String> proxyMap = new ConcurrentHashMap<>();


    /**
     * 建立连接
     */
    private static Document connect(String key,int page) {
        try {
            String[] split = proxyMap.get(key).split(":");
            int i = page % ua.length;
            return Jsoup.connect(url + page)
                    .timeout(200 * 2000)
                    .proxy(split[0],Integer.parseInt(split[1]))
                    .userAgent(ua[i])
                    .header("referer",url + (page - 1))
                    .get();
        } catch (IOException e) {
            proxyMap.put(key, getProxy());
            return connect(key,page);
        }
    }

    /**
     * 获取所有的省份    Lock-玄清
     * @param
     * @return
     */
    public static void getPhoneList(String key,int a,int b) {
        // 24347
        proxyMap.put(key, getProxy());
        for (int i = a; i <= b; i++) {
            Document connect = connect(key,i);
            Elements phoneTrs = connect.select("#list tr");
            for (Element phone : phoneTrs) {
                PhoneInfo phoneInfo = new PhoneInfo();
                Elements td = phone.select("td");
                String td0 = td.get(0).select("a span").text();
                phoneInfo.setSegment(td0);
                String td1 = td.get(1).select("a span").text();
                phoneInfo.setTop7(td1);
                String td2 = td.get(2).select("a span").text();
                phoneInfo.setProvince(td2);
                String td3 = td.get(3).select("a span").text();
                phoneInfo.setCity(td3);
                String td4 = td.get(4).select("a span").text();
                phoneInfo.setOperator(td4);
                String td5 = td.get(5).select("a span").text();
                phoneInfo.setZipCode(td5);
                String td6 = td.get(6).select("a span").text();
                phoneInfo.setAreaNum(td6);
                String td7 = td.get(7).select("a span").text();
                phoneInfo.setAreaCode(td7);
                System.out.println(JSON.toJSONString(phoneInfo));
            }
        }
    }

    public static void main(String[] args) {
        int page = 7;
        int avg = 7;
        int numThreads = page / avg;
        int startPage = 21001;
        for (int i = 0; i < numThreads; i++) {
            int finalI = i;
            int finalStartPage = startPage;
            int finalEndPage = startPage + avg - 1;
            new Thread(() -> {
                String key = "key" + finalI;
                getPhoneList(key,finalStartPage,finalEndPage);}).start();
            startPage = finalEndPage + 1;
        }
    }

    public static String getProxy() {
            String url = "http://api2.xkdaili.com/tools/XApi.ashx?apikey=XK8D9CD261CFFBF23133&qty=1&format=txt&split=0&sign=65e4e5352f89a41eaa1f059dbe8f8c33";
        String proxy = null;
        try {
            proxy = Jsoup.connect(url).get().text();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
            return proxy;
    }

    private static String[] ua = {
            "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.87 Safari/537.36 OPR/37.0.2178.32",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36",
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586",
            "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko",
            "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)",
            "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)",
            "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0)",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 BIDUBrowser/8.3 Safari/537.36",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36 Core/1.47.277.400 QQBrowser/9.4.7658.400",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 UBrowser/5.6.12150.8 Safari/537.36",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36 SE 2.X MetaSr 1.0",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36 TheWorld 7",
            "Mozilla/5.0 (Windows NT 6.1; W…) Gecko/20100101 Firefox/60.0"};
    @Data
    public static class PhoneInfo{
        /**
         * 号段
         */
        private String segment;

        /**
         * 前7位
         */
        private String top7;

        /**
         * 省份
         */
        private String province;

        /**
         * 城市
         */
        private String city;

        /**
         * 运营商
         */
        private String operator;

        /**
         * 邮编
         */
        private String zipCode;

        /**
         * 区号
         */
        private String areaNum;

        /**
         * 地区代码
         */
        private String areaCode;
    }
}
